<?php

session_start();

// include settings and functions
include "../conn/database.php";
include "functions.php";
include 'UUID.php';

// if the user submit the form
if(isset($_POST['submit'])) {
	// initialize errors values
	$errors=0;
	$errors_text = '';
	// cleanning the post data from hacking attempt
	$post_data = clean($_POST);
	
	// empty post data verification
	if(empty($post_data['email'])
	|| empty($post_data['firstName']) || empty($post_data['lastName'])
	|| empty($post_data['dd']) || empty($post_data['mm'])
	|| empty($post_data['yyyy']) || empty($post_data['gender'])) {
		$errors_text .= "Des champs sont vides<br />";
		$errors++;
	}
	
	//
	if($post_data['addwedding-step'] != 'step-1' && $post_data['addwedding-step'] != 'step-2'){
		$errors_text .= "Error addwedding-step<br />";
		$errors++;
	}
	
	// birthday verification (is valid date)
	if(!checkdate($post_data['mm'], $post_data['dd'], $post_data['yyyy'])) {
		$errors_text .= "La date de naissance n'est pas valide<br />";
		$errors++;
	}
	
	// phone verification (10 digits)
	if(!empty($post_data['phoneNumber'])) {
		if(!check_phoneNumber($post_data['phoneNumber'])) {
			$errors_text .= "Le téléphone est incorrect<br />";
			$errors++;
		}
		if(substr($post_data['phoneNumber'], 0, 2) != '06' && substr($post_data['phoneNumber'], 0, 2) != '07') {
			$errors_text .= "Le téléphone doit commencer par 06 ou 07<br />";
			$errors++;
		}
	}
	
	// email verification
	if(!check_email($post_data['email']) ) {
		$errors_text .= "L'email n'est pas correct<br />";
		$errors++;
	}
	
	// gender verification (is 'MALE' or 'FEMALE')
	if(!check_gender($post_data['gender'])) {
		$errors_text .= "La civilité n'est pas valide<br />";
		$errors++;
	}
	// check if user exists
	if(!check_userexist($post_data['email'])) {
		$errors_text .= "User already exists.<br />";
		$errors++;
	}
	
	// if no errors we proceed
	if($errors == 0) {
		// data correction
		$firstName = ucfirst(strtolower($post_data["firstName"]));
		$lastName = strtoupper($post_data["lastName"]);
		$email = strtolower(filter($post_data["email"]));
		$wid = $_SESSION['wid'];
		
		// generating the user ID
		$guid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', generate_guid(6));
		// generating the validation key for the email
		$key = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bccc', generate_guid(6));
		// generating the user Password
		$password = MD5(generate_guid(6));
		
		$birthDate = $post_data["yyyy"].'-'.$post_data["mm"].'-'.$post_data["dd"];
		$phoneNumber = $post_data["phoneNumber"];
	
		$insert_sql = "
			INSERT INTO `T_PERSON`
				(`PERSON_SID`, `FIRST_NAME`, `SURNAME`, `BIRTHDATE`, `PHONE_NUMBER`, `EMAIL_ADDRESS`, `PASSWORD`, `GENDER`)
			VALUES
				('".$guid."','".$firstName."','".$lastName."','".$birthDate."','".$phoneNumber."','".$email."','"
				.  $password ."','".$post_data['gender']."')"
		;
		
		$insert_sql2 = "
			INSERT INTO `R_WEDDINGS_INCHARGE`
				(`PERSON_SID`, `WEDDING_ID`, `ROLE`, `IS_ACTIVATED`, `ACTIVATE_KEY`)
			VALUES
			('".$guid."','".$wid."','1','1','".$key."')"
		;
		
		$update_sql = "
		UPDATE `T_WEDDINGS` SET
		`LEFT_LIKES`='".$post_data['like']."',`LEFT_DISLIKES`='".$post_data['not_like']."' WHERE `WEDDING_ID`='".$wid."'"
		;
		
		$update_sql2 = "
		UPDATE `T_WEDDINGS` SET
		`RIGHT_LIKES`='".$post_data['like']."',`RIGHT_DISLIKES`='".$post_data['not_like']."' WHERE `WEDDING_ID`='".$wid."'"
		;
		
		//insert in to r_weddings_persons
		if($post_data['gender']=="MALE"){$gender = 0;} else {$gender = 1;};
		$insert_sql3 = "
			INSERT INTO `R_WEDDINGS_PERSONS` 
				(WEDDING_ID , PERSON_ID , RELATION_TYPE, ESPOUSER_SIDE_ID)
			VALUES
				('".$wid."','".$guid."','1','".$gender."')"
		;
			
		if(!mysql_query($insert_sql) || !mysql_query($insert_sql2) || !mysql_query($insert_sql3)) {
			$errors_text .= mysql_error()."<br />";
			$errors++;
		}
		if($post_data['addwedding-step'] == 'step-1') {
			if(!mysql_query($update_sql)) {
				$errors_text .= mysql_error()."<br />";
				$errors++;
			} else {
				$_SESSION['luid'] = $guid;
				$target = "addperson-2.php";
			}
		} elseif($post_data['addwedding-step'] == 'step-2') {
			if(!mysql_query($update_sql2)) {
				$errors_text .= mysql_error()."<br />";
				$errors++;
			} else {
				$_SESSION['ruid'] = $guid;
				$target = "addperson-3.php";
			}
		}
	}
	if($errors == 0) {
		$arr['success'] = "1"; 
        $arr['msg'] = 'Vos informations ont bien été enregistrées.';
		$arr['url'] = $target;
	} else {
		$arr['success'] = "0"; 
        $arr['msg'] = $errors_text;
	}
	echo json_encode($arr);
}
?>